package jam;

import robocode.HitByBulletEvent;
import robocode.HitRobotEvent;
import robocode.Robot;
import robocode.ScannedRobotEvent;
import static robocode.util.Utils.normalRelativeAngleDegrees;

/**
 * UPDATE: Inserted new method that regulates the robot firePower according to various distances.
 * Similar to sample robot walls, instead when it is hit by bullet it will try to dodge and take
 * another path.
 * 
 * @author john mack
 */

public class RunScared extends Robot {
  double moveAmount;// store how much the must move on the battlefield
  double gunTurnAmt;// determines how much to turn the gun
  int dist = 200;// how far it will move when hit by bullet

  /**
   * Moves counter clockwise in a square utilizing 1/4 of the battlefield. Unless hit by a bullet.
   */

  public void run() {
    // moveAmount retrieves the entire width of battlefield and entire height / 2 of battlefield.

    moveAmount = Math.max(getBattleFieldWidth() / 2, getBattleFieldHeight() / 2);
    turnLeft(getHeading());
    ahead(moveAmount);
    turnLeft(90);

    while (true) {
      ahead(moveAmount);
      turnLeft(90);

    }
  }

  /**
   * Once a robot is found it will get the distance of the robot and use stronger fire power in
   * closer range and weaker fire power at a distant target.
   * 
   * @param aRobot contains information about the enemies location
   */
  public void onScannedRobot(ScannedRobotEvent aRobot) {
    double distance = aRobot.getDistance(); // stores distance of scanned robot
    // determines appropriate fire power
    fire(firePower(distance));

  }

  /**
   * Will move perpendicular to bullet and move back a distance of 200 pixels.
   * 
   * @param e contains information about the enemies bullet
   */

  public void onHitByBullet(HitByBulletEvent e) {
    turnRight(normalRelativeAngleDegrees(90 - (getHeading() - e.getHeading())));
    back(dist);
    dist *= -1;

  }

  /**
   * If contact is made with another robot it make the robot its new target. Also it will turn it's
   * gun in the direction of the enemy and begin shooting with strong fire power while moving back
   * 50 pixels
   * 
   * @param e contains information various about the enemy robot.
   */

  public void onHitRobot(HitRobotEvent e) {
    gunTurnAmt = normalRelativeAngleDegrees(e.getBearing() + (getHeading() - getRadarHeading()));
    turnGunRight(gunTurnAmt);
    fire(3);
    back(50);
    turnGunLeft(gunTurnAmt);

  }

  /**
   * Checks the distance of an enemy robot and uses appropriate fire power.
   * 
   * @param distance calculates distance of enemy robot and uses appropriate fire power.
   * @return integer specifying fire power.
   */
  public int firePower(double distance) {
    if (distance < 100) {
      return 3;
    }
    else if (distance < 1000) {
      
      return 2; // far distance fire power is 2
    }
    return 1;

  }

}
